package mebp.genetic.recombination;

import java.util.Vector;

import mebp.genetic.Individuum;

public class OnePointCrossover implements RecombinationStrategy {

	@Override
	public Vector<Individuum> recombine(Individuum... parents) {
		Individuum i1 = parents[0];
		Individuum i2 = parents[1];
		int genomLength = i2.genom.length;

		int child1[] = new int[genomLength];
		int child2[] = new int[genomLength];

		Integer splitAt = (int) Math.round(generateRandomNumber(genomLength));

		for (int k = 0; k < splitAt; k++) {
			child1[k] = i1.genom[k];
			child2[k] = i2.genom[k];
		}
		for (int j = splitAt; j < genomLength; j++) {
			child1[j] = i2.genom[j];
			child2[j] = i1.genom[j];
		}

		Vector<Individuum> result = new Vector<Individuum>();
		Individuum in=new Individuum(i1.problem, child1);
		in.repair();
		result.add(in);
		in= new Individuum(i1.problem, child2);
		in.repair();
		result.add(in);
		return result;
	}

	private double generateRandomNumber(int genomLength) {
		double random;
		while (true) {
			random = Math.random() * genomLength;
			if (random > genomLength - 1)
				continue;
			if (random < 1)
				continue;
			break;
		}
		return random;
	}
}
